package com.baidu.sggc.third.mb.model;

import com.alibaba.fastjson.JSONObject;
import com.baidu.sggc.core.common.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class MbModelApi {

  @Value("${third.mb.modelListApi}")
  private String modelListApiPath;

  @Autowired
  private RestTemplate restTemplate;

  @Autowired
  private TokenUtils tokenUtils;

   public ModelListRespDto queryModel(ModelListReqDto reqDto) {
     // 构建查询参数
     JSONObject jsonObject =new JSONObject();
     /*   jsonObject.put("organizationId", reqDto.getOrganizationId());
     jsonObject.put("projectId", reqDto.getProjectId());
     jsonObject.put("nameFilter", reqDto.getNameFilter());*/
     jsonObject.put("pageNo", 1);
     jsonObject.put("pageSize", 1000);
     jsonObject.put("orderBy", "publish");
     jsonObject.put("assetType", "Model");

     // 构建请求实体
     // HttpHeaders headers = tokenUtils.getHeaders(url, "");
     // headers.setContentType(MediaType.APPLICATION_JSON);
     // HttpEntity<?> entity = new HttpEntity<>(headers);


     HttpHeaders headers = new HttpHeaders();
     headers.setContentType(MediaType.APPLICATION_JSON);
     HttpEntity<String> requestEntity = new HttpEntity<>(jsonObject.toString(), headers);
     // 发送GET请求
     ResponseEntity<ModelListRespDto> response = restTemplate.postForEntity(modelListApiPath,
         requestEntity,
         ModelListRespDto.class
     );

     // 处理响应
     if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
       return   response.getBody();
     } else {
       System.out.println("Request failed with status code: " + response.getStatusCode());
     }
     return null;
   }

  /* public String getSk(String userId, String sk) {
     OkHttpClient client = new OkHttpClient().newBuilder()
         .build();
     MediaType mediaType = MediaType.parseMediaType("application/json");
     JSONObject json = new JSONObject();
     json.put("userId", userId);
     json.put("sk", sk);
     Request request = new Request.Builder()
         .url("http://test-cn.your-api-server.com/gallery/api/v1/asset/client/ak")
         .method("POST",mediaType json.toJSONString()))
         .addHeader("User-Agent", "iAPI/1.0.0 (http://iapi.baidu-int.com)")
         .addHeader("Content-Type", "application/json")
         .build();
     Response response = client.newCall(request).execute();
   }*/
}
